/*
 *所有关于dj_mara类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*dj_maraService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using DjErp.IRepositories;
using System.Collections.Generic;
using System;
using VOL.Core.DBManager;
using DjErp.Repositories;

namespace DjErp.Services
{
    public partial class dj_maraService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Idj_maraRepository _repository;//访问数据库
        private WebResponseContent webResponse = new WebResponseContent();
        [ActivatorUtilitiesConstructor]
        public dj_maraService(
            Idj_maraRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        public override WebResponseContent Import(List<IFormFile> files)
        {
            ImportOnExecuting = (List<dj_mara> list) =>
            {
                if (list != null)//根据matnr物料号，如果数据库中有就更新，没有就插入
                {
                    List<dj_mara> addlist = new List<dj_mara>();//新增List
                    List<dj_mara> uplist = new List<dj_mara>();//更新List
                    foreach (var item in list)
                    {
                        var TTT = DBServerProvider.DbContext.Set<dj_mara>().Where(w => w.matnr == item.matnr).FirstOrDefault();
                        if (TTT != null)
                        {
                            item.dj_mara_id = TTT.dj_mara_id;
                            uplist.Add(item);
                        }
                        else
                        {
                            addlist.Add(item);
                        }
                    }
                    //过滤重复数据
                    if (addlist.Count() > 0)
                    {
                        addlist = addlist.GroupBy(x => x.matnr).Select(y => y.First()).ToList();
                        repository.AddRange(addlist, true);
                    }
                    foreach (var item in uplist)
                    {
                        repository.Detached(item);//efcore取消跟踪，需要放在数据库查询tolist()前面
                    }
                    if (uplist.Count() > 0)
                    {
                        uplist = uplist.GroupBy(x => x.matnr).Select(y => y.First()).ToList();
                       
                        repository.UpdateRange(uplist, true);
                    }
                }
                webResponse.Code = "-1";//-1表示不执行后面的update，测试强制返回
                return webResponse.OK("导入成功！");
            };

            return base.Import(files);
        }
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            AddOnExecuting = (dj_mara curr, object list) =>
            {
                var TTT = DBServerProvider.DbContext.Set<dj_mara>().Where(w => w.matnr == curr.matnr).FirstOrDefault();
                if (TTT!=null)
                {
                    return webResponse.Error(curr.matnr+"，该物料号已经存在！");
                }
                return webResponse.OK();
            };
                return base.Add(saveDataModel);
        }
        public override WebResponseContent Update(SaveModel saveModel)
        {
            UpdateOnExecuted = (dj_mara curr, object addList, object updateList, List<object> delKeys) =>
            {
                //需在UpdateOnExecuted事件中，判断总数不能>1
                var TTT = DBServerProvider.DbContext.Set<dj_mara>().Where(w => w.matnr == curr.matnr).ToList();
                if (TTT.Count>1)
                {
                    return webResponse.Error(curr.matnr + "，该物料号已经存在！");
                }
                return webResponse.OK();
            };
            return base.Update(saveModel);
        }
    }
}
